﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Billing.Utilities;
using System.Data;
using Billing.Controls;

namespace Billing.Windows
{
    public partial class Payment : Window
    {
        public bool IsExit { get; set; }
        List<PaymentItemControl> mainList = new List<PaymentItemControl>();

        public Payment()
        {
            InitializeComponent();

            try
            {
                lstSupplierName.ItemsSource = Database.GetSuppliers();
                lstSupplierName.DisplayMemberPath = "SupplierName";
                lstSupplierName.SelectedValuePath = "SupplierID";
                lstSupplierName.SelectedIndex = 0;
                lstSupplierName.Focus();

                List<BillDate> billDates = new List<BillDate>();
                for (int cnt = -20; cnt < 2; cnt++)
                {
                    billDates.Add(new BillDate { ActualDate = DateTime.Now.AddDays(cnt), DateString = DateTime.Now.AddDays(cnt).ToString("dddd dd-MMM-yyyy") });
                }
                lstBillDate.ItemsSource = billDates;
                lstBillDate.DisplayMemberPath = "DateString";
                lstBillDate.SelectedValuePath = "ActualDate";
                lstBillDate.SelectedIndex = 20;

                mainList.Add(new PaymentItemControl());
                foreach (DataRow row in Database.GetPayments().Rows)
                {
                    PaymentItemControl item = new PaymentItemControl();
                    item.lblSupplier.Content = row[0].ToString();
                    item.lblBillNo.Content = row[1].ToString();
                    item.lblAmount.Content = row[2].ToString();
                    mainList.Add(item);
                }

                lbPaymentsGrid.ItemsSource = mainList;
            }
            catch (Exception) { }
        }

        private void Window_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Escape)
            {
                this.Close();
            }
            else if (e.Key == Key.F1)
            {
                if (ValidateFields())
                {
                    //Adding Payment
                    int result = Database.SavePayment(lstSupplierName.SelectedValue.ToString(), txtBillNo.Text, txtAmountNo.Text, DateTime.Parse(lstBillDate.SelectedValue.ToString()).ToString("MM-dd-yyyy"));
                    if (result == 1)
                    {
                        lblMessage.Content = "Bill Added Successfully";
                        txtAmountNo.Clear();
                        txtBillNo.Clear();
                        lstSupplierName.SelectedIndex = 0;
                        lstBillDate.SelectedIndex = 20;
                    }
                    else
                    {
                        lblMessage.Content = "Some Error has Occurred";
                    }
                }
            }
        }

        private bool ValidateFields()
        {
            if (lstSupplierName.SelectedIndex == -1)
            {
                lblMessage.Content = "Suppliers are not Added";
                return false;
            }
            else if (txtBillNo.Text.Trim() == string.Empty)
            {
                lblMessage.Content = "Please Enter Bill #";
                txtBillNo.Focus();
                return false;
            }
            else if (txtAmountNo.Text.Trim() == string.Empty)
            {
                lblMessage.Content = "Please Enter Amount";
                txtAmountNo.Focus();
                return false;
            }
            return true;
        }

        private void lstSupplierName_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Right)
                txtBillNo.Focus();
        }

        private void txtBillNo_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Left)
                lstSupplierName.Focus();
            else if (e.Key == Key.Right)
                txtAmountNo.Focus();
        }

        private void txtAmountNo_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Left)
                txtBillNo.Focus();
            else if (e.Key == Key.Right)
                lstBillDate.Focus();
            else
            {
                double value;
                if (!(double.TryParse(txtAmountNo.Text, out value)))
                {
                    txtAmountNo.Clear();
                }
                else
                {
                    txtAmountNo.Text = Math.Round(value, 2).ToString();
                }
            }
        }

        private void lstBillDate_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Left)
                txtAmountNo.Focus();
        }

        private void lstComboBox_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Right || e.Key == Key.Left)
                e.Handled = true;
            else
                e.Handled = false;
        }
    }

    public class BillDate
    {
        public DateTime ActualDate { get; set; }
        public string DateString { get; set; }
    }
}
